package client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class ThreadLogin extends Thread {
	private Socket s;
	private InputStream in;
	private OutputStream out;
	private boolean register;
	private String login;
	private String password;
	private String reponse;

	public String getReponse() {
		return reponse;
	}

	public void setReponse(String reponse) {
		this.reponse = reponse;
	}

	public ThreadLogin(Socket s, String login, String password, boolean register) throws IOException {
		// TODO Auto-generated constructor stub
		this.s = s;
		//in = new DataInputStream(s.getInputStream());
		//out = new DataOutputStream(s.getOutputStream());
		out = s.getOutputStream();
		in = s.getInputStream();
		//in  = new BufferedReader (new InputStreamReader(s.getInputStream()));

		this.register = register;
		this.login = login;
		this.password = password;
	}
	
	public void run()
	{
		System.out.println("ThreadLogin: lancement "+login+","+password+","+register +
				" sur"+s.toString());
		String msg;
		if (register)
		{
			msg = "register:";
		}
		else
		{
			msg = "connect:";
		}
		msg += login + ":" + password;
		try
		{
			System.out.println("ThreadLogin: envoi "+msg);
			//out.writeUTF(msg);
			out.write(msg.getBytes());
			//out.write(msg.getBytes());
			//reponse = in.readUTF();
			byte[] b = new byte[64];
			in.read(b);
			//in.read(c);
			reponse = new String(b);
			System.out.println("ThreadLogin: reception "+reponse);
		}
		catch (IOException ex)
		{
			//
		}
	}
}
